package cn.maxhou.web.interceptors;

import cn.maxhou.entity.SysUser;
import cn.maxhou.util.Constants;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;

public class AuthInterceptor implements HandlerInterceptor {
    private static Logger LOGGER= LoggerFactory.getLogger(AuthInterceptor.class);
    /**前置处理方法，进入控制器之前经过此方法判断是否能够继续请求*/
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        LOGGER.debug("进入自定义拦截器AuthInterceptor,请求路径:{}",request.getRequestURI());
        HttpSession session=request.getSession();
        //获取登录用户信息
        SysUser user=(SysUser)session.getAttribute(Constants.SESSION_USER);
        //检测是否登录
        if(null==user){
            //没有登录信息,跳转到401.jsp错误页
            request.getRequestDispatcher("/WEB-INF/jsp/401.jsp").forward(request, response);
            //返回false表示请求被拒绝,在返回钱需要做其他操作
            return false;
        }
        //返回true表示请求可以通过
        return true;
    }
}
